//
//  Dropbox.m
//  iSeeColor
//
//  Created by Wyatt Gosling on 11/2/12.
//  Copyright (c) 2012 Wyatt Gosling. All rights reserved.
//

#import "Dropbox.h"

@implementation Dropbox
@synthesize restClient;

//Getter method to allocate and initialize the instance variable
- (DBRestClient *)restClient {
    if (!restClient) {
        restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
        restClient.delegate = self;
    }
    return restClient;
}

//Method to save the image
-(void) save:(UIImage *)image
{
    NSData   *data = UIImageJPEGRepresentation(image, 1.0f);
    NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:@"upload.png"];
    
    [data writeToFile:file atomically:YES];
    [[self restClient] uploadFile:@"upload.png" toPath:@"/Photos" withParentRev:nil fromPath:file];
}

//method to update the status of a dropbox update
#pragma mark DBRestClientDelegate methods
-(void) restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath from:(NSString*)srcPath metadat:(DBMetadata*)metadata
{
    NSLog(@"File uploaded to %@", metadata.path);
}

//method to update if the file being uploaded failed to do so.
-(void) restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error
{
    NSLog(@"File upload failed with error: %@", error);
}

@end
